package com.pirestupppai.ui.activity;

import android.content.Context;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.pirestupppai.R;
import com.pirestupppai.adapter.PsListAdapter;
import com.pirestupppai.base.BaseActivity;
import com.pirestupppai.bean.FeedItem;
import com.pirestupppai.widgets.view.EmptyView;
import com.pirestupppai.widgets.view.LoadMoreFooter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 收藏列表
 *
 * @author: xiaoluo
 * @date: 2017-04-21 15:33
 */
public class CollectionActivity extends BaseActivity {

    @BindView(R.id.collection_recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.collect_empty_view)
    EmptyView emptyView;
    @BindView(R.id.collect_swipe_layout)
    SwipeRefreshLayout swipeRefreshLayout;

    private LoadMoreFooter mFooterView;
    private PsListAdapter mCollectionAdapter;
    private LinearLayoutManager mLinearLayoutManager;

    private long mLastRefreshTime = 0;
    private int mPosition = 0;  // 列表最后一位
    private int mPage = 1;
    private int mSize = 15;
    private boolean hasMore = true;
    private List<FeedItem> mCollectionList = new ArrayList<>();


    @Override
    protected Context getActivityContext() {
        return this;
    }

    @Override
    protected int loadParentLayout() {
        return R.layout.activity_collection;
    }

    @Override
    protected void initViews() {
//        mFooterView = new LoadMoreFooter(mContext);
//        mCollectionAdapter = new PsListAdapter(mContext, PsListAdapter.PS_TYPE_COLLECTION, mCollectionList);
//        mCollectionAdapter.setFooterView(mFooterView);
//        mLinearLayoutManager = new LinearLayoutManager(mContext);
//        mLinearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
//
//        recyclerView.setLayoutManager(mLinearLayoutManager);
//        recyclerView.setAdapter(mCollectionAdapter);
//        recyclerView.setHasFixedSize(true);
    }

    @Override
    protected void initListeners() {
        // 监听到达底部事件
//        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
//            @Override
//            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
//                super.onScrolled(recyclerView, dx, dy);
//                //到达底部时,加载更多
//                int lastVisiblePosition = mLinearLayoutManager.findLastCompletelyVisibleItemPosition() + 1;
//                if (mLinearLayoutManager.getChildCount() > 0
//                        && mLinearLayoutManager.getItemCount() >= mLinearLayoutManager.getChildCount()
//                        && lastVisiblePosition >= mLinearLayoutManager.getItemCount()
//                        && dy > 0) {
//                    mPosition = lastVisiblePosition;
//
//                    if (hasMore) {
//                        mFooterView.setStatus(LoadMoreFooter.Status.LOADING);
//                        loadMore();
//                    }
//                }
//            }
//        });
    }

    @Override
    protected void initLogic() {
//        swipeRefreshLayout.post(new Runnable() {
//            @Override
//            public void run() {
//                swipeRefreshLayout.setRefreshing(true);
//                refresh();
//            }
//        });
    }

    @Override
    public void onClick(View v) {

    }

    /**
     * 下拉刷新
     */
//    private void refresh() {
//        hasMore = true;
//        mPage = 1;
//        mLastRefreshTime = System.currentTimeMillis();
//        mFooterView.setStatus(LoadMoreFooter.Status.GONE);
//
//        NetworkRequest.getInstance().getCollectionList(mLastRefreshTime, mPage, mSize, this,
//                new TupaiSubscriber<List<PhotoItem>>(mContext) {
//                    @Override
//                    protected void requestSuccess(List<PhotoItem> photoItems) {
//                        swipeRefreshLayout.setRefreshing(false);
//                        swipeRefreshLayout.setEnabled(false);
//                        if (photoItems.size() != 0) {
//                            swipeRefreshLayout.setVisibility(View.VISIBLE);
//                            emptyView.setVisibility(View.GONE);
//                            mCollectionList.clear();
//                            mCollectionList.addAll(photoItems);
//                            mCollectionAdapter.notifyDataSetChanged();
//                        } else {
//                            swipeRefreshLayout.setVisibility(View.GONE);
//                            emptyView.setVisibility(View.VISIBLE);
//                        }
//
//                        if (photoItems.size() < mSize) {
//                            mFooterView.setStatus(LoadMoreFooter.Status.THE_END);
//                            hasMore = false;
//                        }
//                    }
//
//                    @Override
//                    protected void requestError(String error) {
//                        swipeRefreshLayout.setRefreshing(false);
//                        swipeRefreshLayout.setEnabled(false);
//                    }
//                });
//
//
//    }
//
//    /**
//     * 上拉加载
//     */
//    private void loadMore() {
//        mPage++;
//
//        NetworkRequest.getInstance().getCollectionList(mLastRefreshTime, mPage, mSize, this,
//                new TupaiSubscriber<List<PhotoItem>>(mContext) {
//                    @Override
//                    protected void requestSuccess(List<PhotoItem> photoItems) {
//                        if (photoItems.size() != 0) {
//                            mCollectionList.addAll(photoItems);
//                            mCollectionAdapter.notifyItemInserted(mPosition);
//                            mFooterView.setStatus(LoadMoreFooter.Status.GONE);
//                        } else {
//                            mFooterView.setStatus(LoadMoreFooter.Status.THE_END);
//                            hasMore = false;
//                        }
//
//                        if (photoItems.size() < mSize) {
//                            mFooterView.setStatus(LoadMoreFooter.Status.THE_END);
//                            hasMore = false;
//                        }
//                    }
//
//                    @Override
//                    protected void requestError(String error) {
//                        mFooterView.setStatus(LoadMoreFooter.Status.GONE);
//                    }
//                });
//
//
//    }
}
